/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.commons.collections.FixedSizeStack;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.transition.Transition;

import java.util.ArrayList;

public class Director extends EngineObject {

    private Scene currentScene;
    private Scene nextScene;
    private boolean doNextScene;
    private boolean doInit;

    float timeScale = 1.0F;

    private Transition activeTransition;

    private final ArrayList<Scene> scenes = new ArrayList<Scene>(6);

    //todo stack
    private final FixedSizeStack<Scene> sceneStack = new FixedSizeStack<Scene>(10);

    public void tick() {

        if (doNextScene) {
            next();
        }

        if (doInit) {
            int s = scenes.size();
            if (s > 0) {
                for (int i = 0; i < s; i++) {
                    Scene scene = scenes.get(i);
                    if (!scene.isInit()) {
                        scene.init();
                    }
                }
            }
        }
    }

    public Scene getCurrentScene() {
        return currentScene;
    }

    public void runScene(Scene scene) {
        nextScene = scene;
        doNextScene = true;
    }

    public void runScene(String tag) {
        Scene scene = getScene(tag);
        if (scene != null) {
            runScene(scene);
        }
    }

    public void addScene(Scene scene) {
        scene.setActive(false);
        scenes.add(scene);
    }

    public void addScene(String tag, Scene scene) {
        scene.setTag(tag);
        addScene(scene);
    }

    public Scene getScene(String tag) {
        for (int i = 0; i < scenes.size(); i++) {
            Scene scene = scenes.get(i);
            if (scene.getTag().equals(tag))
                return scene;
        }
        return null;
    }

    public void render() {
        if (currentScene != null)
            currentScene.render();
    }



    private void next() {
        doNextScene = false;
        if (currentScene == null) {
            if (nextScene != null) {
                currentScene = nextScene;
                nextScene = null;
                currentScene.enter();
            } else {
                engine.log("Director", "No scene available to show next.");
            }
        } else {
             currentScene.exit();
            if (nextScene != null) {
                nextScene.enter();
                currentScene = nextScene;
            } else {
                engine.log("Director", "No next scene available.");
            }
        }
    }

    public float getTimeScale() {
        return timeScale;
    }

    public void setTimeScale(float timeScale) {
        this.timeScale = timeScale;
    }

    public boolean isDoInit() {
        return doInit;
    }

    public void initScenes() {
        this.doInit = true;
    }

    public void pause() {

    }

    public void freeze() {

    }
}
